﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xinkui.WebModel.BasicPage;
using CommonFramework.Utility;
using Xinkui.Entity.AutoGenerator;
using Xinkui.WebModel;
using Xinkui.WebModel.Helper;
using Xinkui.Common;
using Xinkui.DataAccess.AutoGenerator;
using Xinkui.WebModel.Model;
using Xinkui.DataAccess.User;

public partial class postbuyleads : ForePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Enqueue(NavItem.Create("Post a buying lead"));
    }

    protected override void HandlePostActions()
    {
        string title = string.Format("Buy {0}", WebHelper.GetFormValue<string>("txtBuyleadsName"));
        string detail = WebHelper.GetFormValue<string>("txtBuyleaderDetail");
        string email = WebHelper.GetFormValue<string>("txtEmail");
        string first = WebHelper.GetFormValue<string>("txtFirstName");
        string last = WebHelper.GetFormValue<string>("txtLastName");
        string code = WebHelper.GetFormValue<string>("txtCode");
        string category = WebHelper.GetFormValue<string>("hidcategory");
        int country = WebHelper.GetFormValue<int>("dropdownCountry", 0);
        string scode = Session["SK_SecrityCode"] as string;
        if (code.Equals(scode))
        {
            DateTime now = DateTime.Now;
            BuyleadInfoMaintain_Entity buyleads = new BuyleadInfoMaintain_Entity();
            buyleads.AdminID = 0;
            buyleads.AdminModifyDate = now;
            buyleads.AuditDate = now;
            buyleads.AuditStatus = (Int32)AuditStatusEnum.New;
            buyleads.CategoryID = ConvertHelper.ToType<int>(category.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault());
            buyleads.CompanyID = 0;
            buyleads.CreateDate = now;
            buyleads.Details = string.Format(@"{0}", detail);
            buyleads.IsDeleted = 0;
            buyleads.Country = country;
            buyleads.Keywords = string.Empty;
            buyleads.MainKeyword = string.Empty;
            buyleads.OEMNO = string.Empty;
            buyleads.OrderQuantity = string.Empty;
            buyleads.Packing = string.Empty;
            buyleads.PriceRange = string.Empty;
            buyleads.PurchaseType = 0;
            buyleads.Quantity = string.Empty;
            buyleads.ReSubmitDate = now;
            buyleads.Subject = title;
            buyleads.UpdateDate = now;
            if (this.LoginUser != null && this.LoginUser.UserID > 0)
            {
                buyleads.UserID = this.LoginUser.UserID;
            }
            else
            {
                UnloginUserInfo userinfo = new UnloginUserInfo();
                userinfo.Source = (short)UnloginSourceEnum.Buyleads;
                userinfo.FirstName = first;
                userinfo.LastName = last;
                userinfo.Email = email;
                userinfo.CreateDate = DateTime.Now;
                userinfo = UserDataAccess.UnloginUserInfoAdd(userinfo);
                buyleads.UserID = userinfo.TempUserId;
            }

            buyleads.ValidDate = now;
            buyleads.ValidTime = 0;

            string fileName = WebHelper.GetFormValue<string>("hidfilepath", "");
            buyleads.PicImgs = fileName;
            Singleton<BuyleadInfoMaintainDataAccess>.Instance.BuyleadInfoMaintain_Add(buyleads);
            //if (!string.IsNullOrEmpty(fileName)) {

            //    Xinkui.Common.FileUpload.UpdateAttachment(fileName, Xinkui.Common.Enum.ESourceType.FromBuyLeads, buyleads.BuyleadID);
            //}

            UIHelper.ShowSuucessTips("Buyleads has saved");
        }
        else
        {
            UIHelper.ShowErrorTips("Verification Code Not Matched"); return;
        }
    }
}